<template>
  <div class="main">
    <Navigation ref="navigation"></Navigation>
    <scroll-container ref="scrollContainer">
      <div class="container-list">
        <multi-list @scrollTo="scrollTo" :groupDatas="groupDatas" :head="head">
          <imglist_w384
            :groupData="groupDatas.dataArr[0]"
            :focArr="groupDatas.focArr[0]"
            :group="groups[0]"
          ></imglist_w384>
        </multi-list>
      </div>
    </scroll-container>
  </div>
</template>

<script>
import scrollContainer from "@/components/base/scroll-container/scroll-container";
import multiList from "@/components/base/multi-list/multi-list";
import Navigation from "@/components/Navigation";
import imglist_w384 from "../ImgList_w384";

export default {
  name: "xyaq",
  components: {
    Navigation,
    scrollContainer,
    multiList,
    imglist_w384
  },
  data() {
    return {
      groups: [0],
      scrollY: 0,
      backObjName: "xyaq",
      hs: this.$store.state.hs,
      bridge: this.$store.state.bridge,
      folderCode: '',
      head: {
        left: null,
        top: "100-4"
      },
      groupDatas: {
        dataArr: [
          {
            maxLine: 4,
            dynamic: true,
            title: "",
            data: [],
            focObj: []
          }
        ],
        focArr: [[]]
      }
    };
  },
  methods: {
    scroll(pos) {
      this.scrollY = pos.y;
      // console.log(pos);
    },
    scrollTo(targetIndex) {
      //console.log("targetIndex", targetIndex);
      this.$refs.scrollContainer.scrollTo(targetIndex);
    },
    setBackObj(backObj) {
      this.hs.cookie.set(this.backObjName, JSON.stringify(backObj));
    },
    goPlay(folderCode, contentId, contentType){
      // this.$store.commit("setAppShow", false);
      let url = "http://ips-4k.hzdtv.tv/dataquery/gotoAuthPlay?folderCode=" + folderCode + "&contentId=" + contentId
        + "&contentType=" + contentType;
      this.hs.ajax.get(url, (_responseText) => {
        let res = JSON.parse(_responseText);
        //console.log(res);
        if (res.result == "0" || res.result == 0) {
          if (res.authResult == 0) {
            let url = "http://ips-4k.hzdtv.tv/dataquery/contentPlayUrl?folderCode=" + folderCode + "&contentId=" + contentId
              + "&contentType=" + contentType + '&playUrlType=rtsp';
            this.hs.ajax.get(url, (_responseText) => {
              let res = JSON.parse(_responseText);
              this.bridge.gotoApplication2play(res.playUrl)
            }, 0, 3000);
          } else {
            this.bridge.gotoApplication2order({
              ppvId: res.ppvId,
              columnAlias: this.folderCode
            })
          }
        }
      }, 0, 3000);
    },
    getData1() {
      this.hs.ajax.get(
        "http://" +
        this.$store.state.domain +
        "/SMSCH-STB/column/find?columnId=12",
        data => {
          data = JSON.parse(data);
          // console.log(data);
          if (data.head.code === 1) {
            this.hs.ajax.get(
              "http://" +
              this.$store.state.domain +
              "/SMSCH-STB/material/list?columnId=" +
              data.body.id +
              "&pageNum=1&pageSize=1" +
              "&schoolId=" +
              this.$store.state.school.id,
              data => {
                data = JSON.parse(data);
                // console.log(data);
                if (data.head.code === 1) {
                  this.folderCode = data.body[0].more.url;
                }
              },
              0,
              function(msg) {},
              3000
            );
          }
        },
        0,
        function(msg) {},
        3000
      );

      if(!this.folderCode) return

      let url =
        "http://ips-4k.hzdtv.tv/dataquery/folderContents?folderCode=" +
        this.folderCode +
        "&fields=viewpoints&pageItems=20&cImageMode=384,214,JPG&segmentIndex=1&pageIndex=1";
      this.hs.ajax.post(
        "http://125.210.113.145/WSL/Core/CrossDomain.ashx",
        "url=" +
        encodeURIComponent(url) +
        "&data=&method=get&type=default",
        obj => {
          obj = JSON.parse(obj);
          //console.log(obj);
          if (obj.contents.length > 0) {
            this.groupDatas.dataArr[0].title = "";
            obj.contents.forEach((item, index) => {
              this.groupDatas.dataArr[0].data.push({
                name: obj.contents[index].name,
                img: {
                  fhd: obj.contents[index].images[0].url
                },
                base: {
                  createUser: null,
                  createTime: obj.contents[index].createTime.substring(
                    0,
                    8
                  )
                },
                contentId: obj.contents[index].contentId,
                contentType: obj.contents[index].contentType
              });
            });
          }
        }, 0, function(msg) {}, 3000
      );
    },
    navigationClick(id) {
      // 埋点
      this.hs.ajax.get(
        "http://" + this.$store.state.domain + "/SMSCH-STB/column/click?navigationId=" + id,
        data => {
        }, 0, function (msg) {
        }, 3000);
    }
  },
  created() {
    this.getData1();
    this.navigationClick(4)
  },
  mounted() {
    // 导航滚动
    this.$refs.navigation.$refs.scrollContainer.scrollBy(-280, true);

    this.hs.addSelect(this.groups, (group, objid, objindex) => {
      // console.log(this.imgListData[group]);
      this.goPlay(this.folderCode,
        this.groupDatas.dataArr[0].data[objindex].contentId,
        this.groupDatas.dataArr[0].data[objindex].contentType)
    });

    this.hs.initFocus("100-5");

    //  查看是否有记忆焦点
    if (this.hs.cookie.get(this.backObjName)) {
      let backObj = JSON.parse(this.hs.cookie.get(this.backObjName));
      let focArr = backObj.backFocArr;
      this.hs.cookie.delOne(this.backObjName);
      //console.log(JSON.stringify(focArr) + "**************");
      this.hs.initFocus(focArr[0], true);
    }

    this.bridge.whenKeyBack = () => {
      // 当页面显示的时候就返回首页, 隐藏的时候, 就显示页面
      if (this.$store.state.appShow) {
        this.$bridge.callHandler(
          "wasu_function_gotoApplication",
          {
            packageName: "com.wasu.launcher",
            className: "com.wasu.launcher.MainActivity"
          },
          function(response) {}
        );
      } else {
        this.$store.commit("setAppShow", true);
      }
    };
  }
};
</script>

<style scoped lang="less">
.main {
  position: absolute;
  left: 0px;
  top: 0px;
  width: 1920px;
  height: 1080px;
  .content {
    position: absolute;
    left: 0px;
    width: 1920px;
    height: 1080px;
    overflow: hidden;
    transition-duration: 400ms;
    -webkit-transition-duration: 400ms;
    .container-list {
      padding-top: 235px;
      margin: 0 125px;
      .category-list {
        position: relative;
        display: flex;
        margin-left: -48px;
        margin-right: -48px;
        .category {
          width: 384px;
          height: 214px;
          border-radius: 8px;
          margin-left: 48px;
        }
      }
      .nav1_img {
        border-radius: 8px;
      }
      .nav1_foc {
        border: 2px solid #ffffff;
        border-radius: 8px;
        transform: scale(1.1);
        -webkit-transform: scale(1.1);
      }
    }
  }
}
</style>
